package com.tjcm.tjcminspectmanager;

import java.util.Date;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.DateUtilities;

public class GPSQueryActivity extends BaseActivity
{

    private LocationManager  mgr      = null;
    private LocationListener listener = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (!LoginActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, LoginActivity.class);
            return;
        }
        setContentView(R.layout.gpsquery);
        final TextView lon = (TextView) findViewById(R.id.lon);
        final TextView lat = (TextView) findViewById(R.id.lat);
        final TextView time = (TextView) findViewById(R.id.time);
        Button back = (Button) findViewById(R.id.back);
        time.setText(R.string.gpsquery_query_process);
        lon.setText(R.string.gpsquery_query_process);
        lat.setText(R.string.gpsquery_query_process);
        mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new LocationListener()
        {
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                // TODO Auto-generated method stub
                time.setText(R.string.gpsquery_query_process);
                lon.setText(R.string.gpsquery_query_process);
                lat.setText(R.string.gpsquery_query_process);
            }

            @Override
            public void onProviderEnabled(String provider)
            {
                // TODO Auto-generated method stub
                time.setText(R.string.gpsquery_query_process);
                lon.setText(R.string.gpsquery_query_process);
                lat.setText(R.string.gpsquery_query_process);
            }

            @Override
            public void onProviderDisabled(String provider)
            {
                // TODO Auto-generated method stub
                time.setText(R.string.gpsquery_query_notopen);
                lon.setText(R.string.gpsquery_query_notopen);
                lat.setText(R.string.gpsquery_query_notopen);
            }

            @Override
            public void onLocationChanged(Location location)
            {
                // TODO Auto-generated method stub
                long timeLong = location.getTime();
                double lonDouble = location.getLongitude();
                double latDouble = location.getLatitude();
                time.setText(DateUtilities.getFormatDate(new Date(timeLong), "yyyy-MM-dd HH:mm:ss"));
                lon.setText(lonDouble + "");
                lat.setText(latDouble + "");
            }
        };
        mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, listener);
        back.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (listener != null)
            mgr.removeUpdates(listener);
    }

}